clear all
set more off
set scheme s2color
cd ""

use "figureA13_data.dta", clear

* Generate time-to-event dummies

gen rel_time = mdy(month,day,year) - mdy(csm_month,csm_day,2020)
replace rel_time = -20 if rel_time < -20
replace rel_time = 20  if rel_time > 20
tab rel_time, gen(rel_w)

forvalue i = 1/41 {
local j = `i' - 21
label var rel_w`i' "`j'"
}

******************
* Subfigure (1.a)
******************

reghdfe in_confn rel_w1-rel_w19 rel_w21-rel_w41 rel_w20 wdsp temp prcp, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
est store in_confn

#delimit ;
coefplot in_confn, 
         vertical recast(scatter) keep(rel_w*) omitted
         order(rel_w1 rel_w2 rel_w3 rel_w4 rel_w5 rel_w6 rel_w7 rel_w8 rel_w9 rel_w10
               rel_w11 rel_w12 rel_w13 rel_w14 rel_w15 rel_w16 rel_w17 rel_w18 rel_w19 rel_w20 
               rel_w21 rel_w22 rel_w23 rel_w24 rel_w25 rel_w26 rel_w27 rel_w28 rel_w29 rel_w30
               rel_w31 rel_w32 rel_w33 rel_w34 rel_w35 rel_w36 rel_w37 rel_w38 rel_w39 rel_w40 rel_w41)
         msymbol(Oh) mcolor(gs5) pstyle(p4) ciopts(recast(rcap) lpattern(dash) lcolor(gs5))
         xtitle("Relative days to Community Stringent Measures", axis(1)) 
         xlabel(, valuelabel axis(1) labsize(vsmall) tlength(0.8))
         yline(0, lcolor(cranberry) lwidth(thin) lpattern(dash))
         xline(20, lcolor(cranberry) lwidth(thin) lpattern(dash)) yscale(range(-0.8 0.22))
         ylabel(-0.8(0.2)0.2, grid axis(1) labsize(small) tlength(0.8) glwidth(vthin) nogextend) 
         graphregion(fcolor(white) color(white) icolor(white) ilstyle(p1box) margin(small)) plotregion(margin(vsmall));
#delimit cr

******************
* Subfigure (1.b)
******************

* De-trend

reghdfe in_confn rel_time wdsp temp prcp if rel_time < 0, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
gen in_confn_hat = in_confn - _b[rel_time]*rel_time 

* Regress on detrended variable

reghdfe in_confn_hat rel_w1-rel_w19 rel_w21-rel_w41 rel_w20 wdsp temp prcp, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
est store in_confn

#delimit ;
coefplot in_confn, 
         vertical recast(scatter) keep(rel_w*) omitted
         order(rel_w1 rel_w2 rel_w3 rel_w4 rel_w5 rel_w6 rel_w7 rel_w8 rel_w9 rel_w10
               rel_w11 rel_w12 rel_w13 rel_w14 rel_w15 rel_w16 rel_w17 rel_w18 rel_w19 rel_w20 
               rel_w21 rel_w22 rel_w23 rel_w24 rel_w25 rel_w26 rel_w27 rel_w28 rel_w29 rel_w30
               rel_w31 rel_w32 rel_w33 rel_w34 rel_w35 rel_w36 rel_w37 rel_w38 rel_w39 rel_w40 rel_w41)
         msymbol(Oh) mcolor(gs5) pstyle(p4) ciopts(recast(rcap) lpattern(dash) lcolor(gs5))
         xtitle("Relative days to Community Stringent Measures", axis(1)) 
         xlabel(, valuelabel axis(1) labsize(vsmall) tlength(0.8))
         yline(0, lcolor(cranberry) lwidth(thin) lpattern(dash))
         xline(20, lcolor(cranberry) lwidth(thin) lpattern(dash)) yscale(range(-0.8 0.22))
         ylabel(-0.8(0.2)0.2, grid axis(1) labsize(small) tlength(0.8) glwidth(vthin) nogextend) 
         graphregion(fcolor(white) color(white) icolor(white) ilstyle(p1box) margin(small)) plotregion(margin(vsmall));
#delimit cr

****************
* Subfigure (2)
****************

* De-trend for each subgroup

gen in_confn_hat2 = .

reghdfe in_confn rel_time wdsp temp prcp if rel_time < 0 & faction == 1, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
replace in_confn_hat2 = in_confn - _b[rel_time]*rel_time if faction == 1

reghdfe in_confn rel_time wdsp temp prcp if rel_time < 0 & faction == 0, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
replace in_confn_hat2 = in_confn - _b[rel_time]*rel_time if faction == 0

* Regress on detrended variable

qui: reghdfe in_confn_hat2 rel_w1-rel_w19 rel_w21-rel_w41 rel_w20 wdsp temp prcp if faction == 1, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
est store in_confn14_f

qui: reghdfe in_confn_hat2 rel_w1-rel_w19 rel_w21-rel_w41 rel_w20 wdsp temp prcp if faction == 0, absorb(i.city_code i.time i.prov_code#c.time) cluster(time)
est store in_confn14_nf

#delimit ;
coefplot (in_confn14_f,  msymbol(Oh) mcolor(green)  pstyle(p4) ciopts(recast(rcap) lpattern(dash) lcolor(green)))
         (in_confn14_nf, msymbol(Th) mcolor(orange) pstyle(p4) ciopts(recast(rcap) lpattern(dash) lcolor(*0.9))), 
         vertical recast(scatter) keep(rel_w*) omitted
         order(rel_w1 rel_w2 rel_w3 rel_w4 rel_w5 rel_w6 rel_w7 rel_w8 rel_w9 rel_w10
               rel_w11 rel_w12 rel_w13 rel_w14 rel_w15 rel_w16 rel_w17 rel_w18 rel_w19 rel_w20 
               rel_w21 rel_w22 rel_w23 rel_w24 rel_w25 rel_w26 rel_w27 rel_w28 rel_w29 rel_w30
               rel_w31 rel_w32 rel_w33 rel_w34 rel_w35 rel_w36 rel_w37 rel_w38 rel_w39 rel_w40
               rel_w41 rel_w42 rel_w43 rel_w44 rel_w45)
         xtitle("Relative days to Community Stringent Measures") xlabel(, valuelabel labsize(vsmall) tlength(0.8))
         yline(0 , lcolor(cranberry) lwidth(thin) lpattern(dash))
         xline(20, lcolor(cranberry) lwidth(thin) lpattern(dash)) yscale(range(-0.8 0.22))
         ylabel(-0.8(0.2)0.2, labsize(small) tlength(0.8) grid glwidth(vthin) nogextend)
         legend(label(2 "With informal tie") label(4 "Without informal tie") position(8) ring(0) col(1))
         graphregion(fcolor(white) color(white) icolor(white) margin(small)) plotregion(margin(vsmall));
#delimit cr

****************
* Subfigure (3)
****************

* Generate interaction terms between time-to-event dummies and faction

forvalue i = 1/41 {
gen rel_w`i'_D = rel_w`i' * faction
}

forvalue i = 1/41 {
local j = `i' - 21
label var rel_w`i'_D "`j'"
}

gen treat_csm = rel_time > 0

qui: reghdfe in_confn_hat2 rel_w1_D-rel_w19_D rel_w21_D-rel_w41_D rel_w20_D treat_csm wdsp temp prcp, absorb(i.city_code#i.faction i.time#i.faction i.prov_code#c.time#i.faction) cluster(time)
est store in_confn14_ddd

#delimit ;
coefplot in_confn14_ddd, vertical recast(scatter) keep(rel_w*_D) omitted
                  order(rel_w1_D rel_w2_D rel_w3_D rel_w4_D rel_w5_D rel_w6_D rel_w7_D rel_w8_D rel_w9_D rel_w10_D
                        rel_w11_D rel_w12_D rel_w13_D rel_w14_D rel_w15_D rel_w16_D rel_w17_D rel_w18_D rel_w19_D rel_w20_D 
                        rel_w21_D rel_w22_D rel_w23_D rel_w24_D rel_w25_D rel_w26_D rel_w27_D rel_w28_D rel_w29_D rel_w30_D
                        rel_w31_D rel_w32_D rel_w33_D rel_w34_D rel_w35_D rel_w36_D rel_w37_D rel_w38_D rel_w39_D rel_w40_D
                        rel_w41_D rel_w42_D rel_w43_D rel_w44_D rel_w45_D)
                  msymbol(Oh) mcolor(blue) pstyle(p4) ciopts(recast(rcap) lpattern(dash) lcolor(blue))
                  xtitle("Relative days to Community Stringent Measures", axis(1)) 
                  xlabel(, valuelabel axis(1) labsize(vsmall) tlength(0.8))
                  yline(0 , lcolor(cranberry) lwidth(thin) lpattern(dash))
                  xline(20, lcolor(cranberry) lwidth(thin) lpattern(dash)) yscale(range(-0.8 0.22))
                  ylabel(-0.8(0.2)0.2, grid axis(1) labsize(small) tlength(0.8) glwidth(vthin) nogextend) 
                  graphregion(fcolor(white) color(white) icolor(white) ilstyle(p1box) margin(small)) plotregion(margin(vsmall));
#delimit cr
